package com.xjy.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.annotation.DataScope;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.utils.SecurityUtils;
import com.xjy.system.domain.SysProj;
import com.xjy.system.domain.bo.*;
import com.xjy.system.domain.vo.SysProjVo;
import com.xjy.system.mapper.SysProjMapper;
import com.xjy.system.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 项目基本信息Service业务层处理
 *
 * @author xjy
 * @date 2022-02-08
 */
@Service
public class SysProjServiceImpl extends ServicePlusImpl<SysProjMapper, SysProj> implements ISysProjService {

	@Autowired
	private ISysDeptService iSysDeptService;
	@Autowired
	private ISysPayaccountService iSysPayaccountService;

	@Autowired
	private ISysBudgetService iSysBudgetService;

	@Autowired
	private ISysTransactionUnitService iSysTransactionUnitService;

	@Autowired
	private ISysImageMngService iSysImageMngService;


	@Override
	@DataScope(proj = "p", deptAlias = "d")
	public TableDataInfo proj(SysProj sysProj) {
		List<SysProjVo> list = baseMapper.proj(sysProj);
		return PageUtils.getPageInfo(list);

	}

	@Override
	@DataScope(proj = "p", deptAlias = "d")
	public List<SysProjVo> listAll(SysProj sysProj) {
		List<SysProjVo> list = baseMapper.proj(sysProj);
		return list;
	}

	@Override
	public SysProjVo queryById(Long id) {
		return getVoById(id, SysProjVo.class);
	}

	@Override
	public Boolean insertByAddBo(SysProjAddBo bo) {
		SysProj add = BeanUtil.toBean(bo, SysProj.class);
		add.setDeptName(iSysDeptService.selectDeptById(bo.getDepart().longValue()).getDeptName());
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysProjEditBo bo) {
		SysProj update = BeanUtil.toBean(bo, SysProj.class);
		update.setDeptName(iSysDeptService.selectDeptById(bo.getDepart().longValue()).getDeptName());
		validEntityBeforeSave(update);
		return updateById(update);
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	private void validEntityBeforeSave(SysProj entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public List<SysProj> listByUser(Long userId) {
		//查询用户部门
		List<Integer> deptIds = iSysDeptService.findDeptIdByUserId(userId);
		if (CollectionUtils.isEmpty(deptIds)) {
			return null;
		}
		LambdaQueryWrapper<SysProj> lmd = Wrappers.lambdaQuery();
		lmd.eq(SysProj::getAuditStatus, true);
		lmd.select(SysProj::getId, SysProj::getName).in(SysProj::getDepart, deptIds).eq(SysProj::getAuditStatus, true);
		return list(lmd);
	}

	@Transactional
	@Override
	public Boolean migrateBaseInfos(SysProjMigrateBaseInfosBo bo) {
		baseMapper.migratePayaccountData(bo);
		baseMapper.migrateBudgetData(bo);
//		baseMapper.migrateTransactionData(bo);
		return true;
	}

	@Override
	public boolean auditById(Long id) {
		SysProj sysProj = baseMapper.selectById(id);
		if (sysProj.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}

		sysProj.setAuditStatus(true);
		boolean rsp = updateById(sysProj);
		if (rsp) {
			this.uploadImage(sysProj);
		}
		return rsp;
	}

	private boolean uploadImage(SysProj bo) {
		boolean rsp = false;
		if(StringUtils.isNotBlank(bo.getFile()) && !"[]".equals(bo.getFile())){
			SysImageMngAddBo sysImageMngAddBo = new SysImageMngAddBo();
			sysImageMngAddBo.setAuditStatus(true);
			sysImageMngAddBo.setProjId(bo.getId());
			sysImageMngAddBo.setImage(bo.getFile());
			sysImageMngAddBo.setType(5 + "");
			sysImageMngAddBo.setOperatorId(SecurityUtils.getLoginUser().getUser().getUserId());
			sysImageMngAddBo.setOperator(SecurityUtils.getLoginUser().getUser().getUserName());
			sysImageMngAddBo.setOutNo(bo.getNumber());

			rsp = iSysImageMngService.insertByAddBo(sysImageMngAddBo);
		}

		return rsp;
	}

	@Override
	public List<Long> getReadableProjIds() {
		Long id = SecurityUtils.getLoginUser().getUser().getUserId();
		if (SecurityUtils.isAdmin(id)) {
			return baseMapper.getReadableProjIds(null);
		} else {
			return baseMapper.getReadableProjIds(SecurityUtils.getLoginUser().getUser().getDeptId());
		}
	}
}

